package Day10;

import java.util.Scanner;

/**
 * @author HYHwtx
 * @version 1.0
 * @date 2022/2/16 11:44
 */

/*
* 动态实现动态的给数组添加元素效果，实现对数组的扩容*/
public class Demo13 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] array = {1, 2, 3};
        do {
            int[] arrayNew = new int[array.length + 1];
            for (int i = 0; i < array.length; i++) {
                arrayNew[i] = array[i];
            }
            System.out.println("请输入您要添加的元素：");
            int addNum = in.nextInt();
            arrayNew[arrayNew.length-1] = addNum;
            array = arrayNew;
            System.out.println("扩容后的情况：");
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i]+" ");
            }
            System.out.println("是否继续添加？Y/N");
            char key = in.next().charAt(0);
            if (key=='N'){
                break;
            }
        }while (true);
        System.out.println("退出添加");
    }
}
